import { Component, OnInit, Injector, ViewChild, EventEmitter, Output, Input } from '@angular/core';
import { appModuleAnimation } from '@shared/animations/routerTransition';
import { AppComponentBase } from '@shared/common/app-component-base';
import { Table } from 'primeng/table';
import { Paginator } from 'primeng/primeng';
import {
    ListDtoExtensionsItem,
    StockZT_InventoryBillItemServiceProxy,
    Stock_InventoryItemListDto,
    Stock_InventoryBillBaseEditDto,
    StockType,
    Stock_AccountBaseListDto
} from '@shared/service-proxies/stock-service-proxies';
import * as _ from 'lodash';
import { element } from '@angular/core/src/render3';
import { ModalDirective } from 'ngx-bootstrap';
//import { Cost_ProdQCCardListDto } from '@shared/service-proxies/stock-service-proxies';

@Component({
    templateUrl: './create-or-edit-inventory-bill-item-modal.component.html',
    animations: [appModuleAnimation()],
    selector: 'createOrEditInventoryBillModal'
})
export class CreateOrEditInventoryBillModalComponent extends AppComponentBase implements OnInit {
    @ViewChild('dataTable') dataTable: Table;
    @ViewChild('paginator') paginator: Paginator;

    @ViewChild('detailDataTable') detailDataTable: Table;
    @ViewChild('detailPaginator') detailPaginator: Paginator;
    @ViewChild('createOrEditModal') modal: ModalDirective;

    @Output() modalSave: EventEmitter<any> = new EventEmitter<any>();
    @Output() modalUpdate: EventEmitter<any> = new EventEmitter<any>();
    @Output() accountShow: EventEmitter<any> = new EventEmitter<any>();
    @Output() clearQuery: EventEmitter<any> = new EventEmitter<any>();
    extensions: Array<ListDtoExtensionsItem>;
    detialExtensions: Array<ListDtoExtensionsItem>;

    _stocktype: StockType;
    @Input() set stocktype(val) {
        if (val) {
            this._stocktype = val;
        }
    }

    selectedBill: Stock_InventoryItemListDto;
    billbase = new Stock_InventoryBillBaseEditDto();

    miliInventoryBillList = new Array<Stock_InventoryItemListDto>();
    f_MaterialTypeOptions;
    recipientsNatureOptions;
    itemid;
    //qccard: Cost_ProdQCCardListDto;
    constructor(injector: Injector, private _billService: StockZT_InventoryBillItemServiceProxy) {
        super(injector);
    }
    active = false;
    saving = false;
    billid = '';
    closed = false;
    show(id?: string, billitem?: Stock_InventoryItemListDto): void {
        this.closed = false;
        this.active = true;
        this.billid = id;
        if (billitem != null || billitem != undefined) {
            this.itemid = billitem.id;
            billitem.f_Sort = 0;
            this.miliInventoryBillList.push(billitem);
        }

        this._billService.getInventoryBillEdit(id).subscribe(result => {
            this.billbase = result;
            this.billbase.f_StockType = this._stocktype;
            this.modal.show();
        });
    }
    close(): void {
        this.active = false;
        this.modal.hide();
    }
    ngOnInit() { }

    onHidden(event) {
        this.closed = true;
    }

    delete() {
        if (!this.selectedBill) {
            this.notify.error('请先勾选对应的物料信息');
            return;
        }

        let miliinventorybill = new Stock_InventoryItemListDto();
        miliinventorybill = _.filter(this.miliInventoryBillList, { f_Sort: this.selectedBill.f_Sort })[0];
        _.remove(this.miliInventoryBillList, miliinventorybill);
        this.selectedBill = null;
    }
    addAccoutInfo(event: Array<Stock_AccountBaseListDto>) {
        console.log(event);
        if (event) {
            event.forEach(element => {
                if (!this.miliInventoryBillList.find(e => element.f_MaterialCode == e.f_MaterialCode)) {
                    let miliinventorybill = new Stock_InventoryItemListDto();
                    miliinventorybill.f_MaterialCode = element.f_MaterialCode;
                    miliinventorybill.f_MaterialName = element.f_MaterialName;
                    miliinventorybill.f_Sort = this.miliInventoryBillList.length;
                    miliinventorybill.f_InventBatchID = element.f_InventBatchID;
                    miliinventorybill.f_WarehouseCode = element.f_WarehouseName;
                    miliinventorybill.f_AccountId = element.id;
                    (miliinventorybill.f_CertificationNum = element.f_CertificationNum),
                        (miliinventorybill.f_Quantity = element.f_RealInventory),
                        (miliinventorybill.f_Unit = element.f_UnitId),
                        (miliinventorybill.f_UnitPrice = element.f_Price);
                    miliinventorybill.f_InventSerialID = element.f_InventSerialID;
                    this.miliInventoryBillList.push(miliinventorybill);
                }
            });
        }

        console.log(this.miliInventoryBillList);
        this.show(this.billid, undefined);
    }

    cancel(): void {
        this.miliInventoryBillList = new Array<Stock_InventoryItemListDto>();
        this.close();
    }

    accountmodalshow() {
        this.close();

        let timer = setInterval(() => {
            if (this.closed) {
                this.accountShow.emit(event);
                clearInterval(timer);
            }
        }, 50);
    }
    save(): void {
        this._billService.createOrUpdateInventoryBillBase(this.billbase).subscribe(result => {
            this._billService.creatInventoryBillItems(this.miliInventoryBillList, result).subscribe(result => {
                if (this.itemid != null && this.itemid != undefined) {
                    this._billService.deleteInventoryBillItem(this.itemid).subscribe(() => {
                        this.notify.success(this.l('更新成功'));
                        this.modalSave.emit(this.billid);
                        this.billid = undefined;
                        this.itemid = undefined;
                        this.close();
                    });
                } else {
                    this.notify.success(this.l('提交成功'));
                    this.clearQuery.emit();
                    this.modalSave.emit(this.billid);
                    this.billid = undefined;
                    this.itemid = undefined;
                    this.close();
                }

                this.miliInventoryBillList = new Array<Stock_InventoryItemListDto>();
            });
        });
    }
}
